//
//  WYMomentHeaderView.m
//  FriendCircleDemo
//
//  Created by mac on 2018/9/27.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentHeaderView.h"
#import "WYMomentPhotosView.h"
#import "WYMomentOperationMoreView.h"
#import "WYMomentShareInfoView.h"
#import "WYMomentVideoView.h"
#import "WYMomentAttitudesItemViewModel.h"
#import "WYMomentCommentItemViewModel.h"

@interface WYMomentHeaderView ()
/// 头像
@property (nonatomic, readwrite, weak) UIImageView *avatarView;
/// 昵称
@property (nonatomic, readwrite, weak) YYLabel *screenNameLable;
/// 正文
@property (nonatomic, readwrite, weak) YYLabel *contentLable;
/// 时间
@property (nonatomic, readwrite, weak) YYLabel *createAtLable;
/// 位置
@property (nonatomic, readwrite, weak) YYLabel *locationLable;
/// 来源
@property (nonatomic, readwrite, weak) YYLabel *sourceLable;
/// 更多按钮
@property (nonatomic, readwrite, weak) UIButton *operationMoreBtn;
/// 更多操作view
@property (nonatomic, readwrite, weak) WYMomentOperationMoreView *operationMoreView;

/// 全文/收起 按钮
@property (nonatomic, readwrite, weak) UIButton *expandBtn;
/// 配图View
@property (nonatomic, readwrite, weak) WYMomentPhotosView *photosView;

/// 分享View
@property (nonatomic, readwrite, weak) WYMomentShareInfoView *shareInfoView;
/// videoView
@property (nonatomic, readwrite, weak) WYMomentVideoView *videoView;
@end

@implementation WYMomentHeaderView

+(instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"MomentHeader";
    WYMomentHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (header == nil) {
        header = [[self alloc] initWithReuseIdentifier:ID];
    }
    return header;
}

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    //// PS:这里把所有可能要显示的View全部初始化好，避免滚动过程中的动态创建，影响性能
    
    /// 用户头像
    UIImageView *avatarView = [[UIImageView alloc] init];
    avatarView.backgroundColor = self.contentView.backgroundColor;
    /// 设置可交互
    avatarView.userInteractionEnabled = YES;
    self.avatarView = avatarView;
    [self.contentView addSubview:avatarView];
    
    /// 昵称
    YYLabel *screenNameLable = [[YYLabel alloc] init];
    screenNameLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向中心对齐
    screenNameLable.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    /// 异步渲染和布局
    screenNameLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    screenNameLable.ignoreCommonProperties = YES;
    screenNameLable.fadeOnAsynchronouslyDisplay = NO;
    screenNameLable.fadeOnHighlight = NO;
    [self.contentView addSubview:screenNameLable];
    self.screenNameLable = screenNameLable;
    
    /// 正文
    YYLabel *contentLable = [[YYLabel alloc] init];
    contentLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向顶部对齐
    contentLable.textVerticalAlignment = YYTextVerticalAlignmentTop;
    /// 异步渲染和布局
    contentLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    contentLable.ignoreCommonProperties = YES;
    contentLable.fadeOnAsynchronouslyDisplay = NO;
    contentLable.fadeOnHighlight = NO;
    contentLable.preferredMaxLayoutWidth = WYMomentCommentViewWidth();//MHMomentCommentViewWidth();
    [self.contentView addSubview:contentLable];
    self.contentLable = contentLable;
    
    /// 展开、关闭按钮
    UIButton *expandBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [expandBtn setTitle:@"全文" forState:UIControlStateNormal];
    [expandBtn setTitleColor:MomentScreenNameTextColor forState:UIControlStateNormal];
    [expandBtn.titleLabel setFont:WYMomentExpandTextFont];
    [expandBtn setBackgroundImage:[UIImage imageWithColor:TextHighlightBackgroundColor size:CGSizeMake(WYMomentExpandButtonWidth, WYMomentExpandButtonHeight)] forState:UIControlStateHighlighted];
    [expandBtn addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
    /// 子控件超出部分裁剪
    expandBtn.clipsToBounds = YES;
    [self.contentView addSubview:expandBtn];
    self.expandBtn = expandBtn;
    
    /// photosView
    WYMomentPhotosView *photosView = [[WYMomentPhotosView alloc] init];
    photosView.clipsToBounds = YES;
    photosView.backgroundColor = self.contentView.backgroundColor;
    self.photosView = photosView;
    [self.contentView addSubview:photosView];
    
    /// shareInfoView
    WYMomentShareInfoView *shareInfoView = [WYMomentShareInfoView shareInfoView];
    self.shareInfoView = shareInfoView;
    [self.contentView addSubview:shareInfoView];
    
    /// videoInfoView
    WYMomentVideoView *videoInfoView = [WYMomentVideoView videoView];
    self.videoView = videoInfoView;
    [self.contentView addSubview:videoInfoView];
    
    /// 位置
    YYLabel *locationLable = [[YYLabel alloc] init];
    locationLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向中心对齐
    locationLable.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    /// 异步渲染和布局
    locationLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    locationLable.ignoreCommonProperties = NO;
    locationLable.fadeOnAsynchronouslyDisplay = NO;
    locationLable.fadeOnHighlight = NO;
    [self.contentView addSubview:locationLable];
    self.locationLable = locationLable;
    
    /// 创建时间
    YYLabel *createAtLable = [[YYLabel alloc] init];
    createAtLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向中心对齐
    createAtLable.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    /// 异步渲染和布局
    createAtLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    createAtLable.ignoreCommonProperties = YES;
    createAtLable.fadeOnAsynchronouslyDisplay = NO;
    createAtLable.fadeOnHighlight = NO;
    [self.contentView addSubview:createAtLable];
    self.createAtLable = createAtLable;
    
    /// 来源
    YYLabel *sourceLable = [[YYLabel alloc] init];
    sourceLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向中心对齐
    sourceLable.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    /// 异步渲染和布局
    sourceLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    sourceLable.ignoreCommonProperties = YES;
    sourceLable.fadeOnAsynchronouslyDisplay = NO;
    sourceLable.fadeOnHighlight = NO;
    [self.contentView addSubview:sourceLable];
    self.sourceLable = sourceLable;
    
    /// 更多操作按钮
    UIButton *operationMoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [operationMoreBtn setImage:[UIImage imageNamed:@"wx_albumOperateMore_25x25"] forState:UIControlStateNormal];
    [operationMoreBtn setImage:[UIImage imageNamed:@"wx_albumOperateMoreHL_25x25"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:operationMoreBtn];
    operationMoreBtn.backgroundColor = self.contentView.backgroundColor;
    self.operationMoreBtn = operationMoreBtn;
    [operationMoreBtn addTarget:self action:@selector(operationMoreBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    /// 更多操作
    WYMomentOperationMoreView *operationMoreView = [[WYMomentOperationMoreView alloc] init];
    //点赞按钮点击
    @weakify(self);
    operationMoreView.attitudesClickedCallback = ^(WYMomentOperationMoreView *operationMoreView) {
        @strongify(self);
        [self attitudesClicked:operationMoreView];
    };
    operationMoreView.commentClickedCallback = ^(WYMomentOperationMoreView *operationMoreView) {
        @strongify(self);
        [self commentClicked:operationMoreView];
    };
    [self.contentView addSubview:operationMoreView];
    self.operationMoreView = operationMoreView;
}

-(void)setItemViewModel:(WYMomentItemViewModel *)itemViewModel
{
    _itemViewModel = itemViewModel;
    /// 头像
    self.avatarView.frame = itemViewModel.avatarViewFrame;
    [self.avatarView setImageURL:itemViewModel.moment.user.profileImageUrl];
    /// 昵称
    self.screenNameLable.textLayout = itemViewModel.screenNameLableLayout;
    self.screenNameLable.frame = itemViewModel.screenNameLableFrame;
    /// 正文
    self.contentLable.textLayout = itemViewModel.contentLableLayout;
    self.contentLable.frame = itemViewModel.contentLableFrame;
    
    /// 全文/收起
    self.expandBtn.frame = itemViewModel.expandBtnFrame;
    [self.expandBtn setTitle:itemViewModel.isExpand?@"收起":@"全文" forState:UIControlStateNormal];
    
    /// 配图
    self.photosView.frame = itemViewModel.photosViewFrame;
    self.photosView.picInfos = itemViewModel.moment.picInfos;
    
    /// 分享
    self.shareInfoView.hidden = !(itemViewModel.moment.type == WYMomentExtendTypeShare);
    self.shareInfoView.frame = itemViewModel.shareInfoViewFrame;
    self.shareInfoView.itemViewModel = itemViewModel;
    
    ///视频
    self.videoView.hidden = !(itemViewModel.moment.type == WYMomentExtendTypeVideo);
    self.videoView.frame = itemViewModel.videoViewFrame;
    self.videoView.itemViewModel = itemViewModel;
    
    /// 位置
    self.locationLable.textLayout = itemViewModel.locationLableLayout;
    self.locationLable.frame = itemViewModel.locationLableFrame;
    
    /// 时间
    self.createAtLable.textLayout = itemViewModel.createAtLableLayout;
    self.createAtLable.frame = itemViewModel.createAtLableFrame;
    
    //来源
    self.sourceLable.textLayout = itemViewModel.sourceLableLayout;
    self.sourceLable.frame = itemViewModel.sourceLableFrame;
    
    /// 更多操作按钮
    self.operationMoreBtn.frame = itemViewModel.operationMoreBtnFrame;
    
    /// 更多View
    self.operationMoreView.right = self.operationMoreBtn.left - WYMomentContentInnerMargin;
    self.operationMoreView.centerY = self.operationMoreBtn.centerY;
    self.operationMoreView.width = 0;
}

-(void)expandAction:(UIButton *)btn
{
    self.itemViewModel.expand = !self.itemViewModel.expand;
    [self.expandBtn setTitle:_itemViewModel.isExpand?@"收起":@"全文" forState:UIControlStateNormal];
    [self.itemViewModel updateSubviewsFrameWithExpand:self.itemViewModel.expand];
    if (self.reloadSection) {
        self.reloadSection(_section);
    }
}

//更多按钮点击
-(void)operationMoreBtnAction:(UIButton *)btn
{
    /// 固定位置
    self.operationMoreView.right = self.operationMoreBtn.left - WYMomentContentInnerMargin;
    self.operationMoreView.isShow?[self.operationMoreView hideWithAnimated:YES]:[self.operationMoreView showWithAnimated:YES];
    self.operationMoreView.itemViewModel = _itemViewModel;
}

//点赞点击事件
-(void)attitudesClicked:(WYMomentOperationMoreView *)operationMoreView
{
    WYMoment *moment = self.itemViewModel.moment;
    if (moment.attitudesStatus <= 0) {
        ///未点赞，则点赞 >0即可
        moment.attitudesStatus = 1;
        moment.attitudesCount += 1;
        [moment.attitudesList addObject:[WYMomentHelper currentUser]];
    }else{
        /// 已点赞，则取消点赞
        moment.attitudesStatus = 0;
        moment.attitudesCount -= 1;
        [moment.attitudesList removeObject:[WYMomentHelper currentUser]];
    }
    /// 0. 还需要判断 self.moment.attitudesList.count > 0 则self.dataSource = nil
    /// 1. 取出dataSource的第一条数据(有可能是nil ，点赞模型，评论模型)
    if (moment.attitudesList.count > 0) {
        /// 有数据
        WYMomentAttitudesItemViewModel *attitudesItemModel = self.itemViewModel.dataSource.firstObject;
        if ([attitudesItemModel isKindOfClass:[WYMomentAttitudesItemViewModel class]]) {
            attitudesItemModel.moment = moment;
        }else{
            /// 插入数据到 index = 0 （创建数据）
            WYMomentAttitudesItemViewModel *atti = [[WYMomentAttitudesItemViewModel alloc] initWithMoment:moment];
            [self.itemViewModel.dataSource insertObject:atti atIndex:0];
        }
    }else{
        /// 这里没有点赞用户，删除掉第一个，
        [self.itemViewModel.dataSource removeFirstObject];
    }
    if (self.reloadSection) {
        self.reloadSection(_section);
    }
}

//评论点击事件
-(void)commentClicked:(WYMomentOperationMoreView *)operationMoreView
{
    
}
@end
